<?php 
require_once 'BaseMySQLDAO.php';
require_once dirname(__FILE__).DIRECTORY_SEPARATOR.'../Entity/Domain/ArticleDomain.php';
class ArticleDAO extends BaseMySQLDAO
{
	public function __construct($context) {
		parent::__construct($context, ArticleDomain::$CollectionName);
	}

	public function create($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "INSERT INTO article(articleID,code,categoryID,nameEN,nameNL,nameDE,nameFR,descriptionEN,descriptionNL,descriptionDE,descriptionFR,width,height,depth,imagePath,price,discount,createDate)
					VALUES	(
								".$obj->getArticleID().",
								'".$obj->getCode()."',
								".$obj->getCategoryID().",
								'".$obj->getNameEN()."',
								'".$obj->getNameNL()."',
								'".$obj->getNameDE()."',
								'".$obj->getNameFR()."',
								'".$obj->getDescriptionEN()."',
								'".$obj->getDescriptionNL()."',
								'".$obj->getDescriptionDE()."',
								'".$obj->getDescriptionFR()."',
								".$obj->getWidth().",
								".$obj->getHeight().",
								".$obj->getDepth().",
								'".$obj->getImagePath()."',
								".$obj->getPrice().",
								".$obj->getDiscount().",
								'".$obj->getCreateDate()."'
							)";
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function deleteByOid($oid){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "DELETE FROM article WHERE articleID=".$oid;
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function update($obj){
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "UPDATE article
				SET
					article.code = '".$obj->getCode()."',
					article.categoryID = ".$obj->getCategoryID().",
					article.nameEN = '".$obj->getNameEN()."',
					article.nameNL = '".$obj->getNameNL()."',
					article.nameDE = '".$obj->getNameDE()."',
					article.nameFR = '".$obj->getNameFR()."',
					article.descriptionEN = '".$obj->getDescriptionEN()."',
					article.descriptionNL = '".$obj->getDescriptionNL()."',
					article.descriptionDE = '".$obj->getDescriptionDE()."',
					article.descriptionFR = '".$obj->getDescriptionFR()."',
					article.width = ".$obj->getWidth().",
					article.height = ".$obj->getHeight().",
					article.depth = ".$obj->getDepth().",
					article.imagePath = '".$obj->getImagePath()."',
					article.price = ".$obj->getPrice().",
					article.discount = ".$obj->getDiscount().",
					article.createDate = '".$obj->getCreateDate()."'
				 WHERE articleID=".$obj->getArticleID();
		$this->getMySQLConnection()->mysqlData($inSQL);
		$this->getContext()->getDBTransaction()->closeTransaction();
	}

	public function findByOid($oid)
	{
		$articleDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM article WHERE article.articleID = ".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$articleDomain = new ArticleDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $articleDomain;
	}

	public function findAll()
	{
		$articleDomainArr = array();
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM article";
		$res = $this->getMySQLConnection()->mysqlData($inSQL);
		if($res)
		{
			while ($data = mysql_fetch_assoc($res))
			{
				$articleDomain = new ArticleDomain($data);
				$articleDomainArr[] = $articleDomain;
			}
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $articleDomain;
	}
	
	public function findByCode($code)
	{
		$articleDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM article WHERE article.`code` = '{$code}'";
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$articleDomain = new ArticleDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $articleDomain;
	}

	public function findShortByOid($oid)
	{
		$articleDomain = null;
		$this->getContext()->getDBTransaction()->startTransaction();
		$inSQL = "SELECT * FROM article WHERE article.articleID = ".$oid;
		$res = $this->getMySQLConnection()->mysqlOneRow($inSQL);
		if($res)
		{
			$articleDomain = new ArticleDomain($res);
		}
		$this->getContext()->getDBTransaction()->closeTransaction();
		return $articleDomain;
	}
}
?>